﻿Public Class ActionConfig

    Private Sub cbActionType_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbActionType.SelectedIndexChanged
        Select Case cbActionType.SelectedIndex
            Case 0 ' Run Event
                txtDeviceCode.Enabled = False
                txtScript.Enabled = True
                cbX10Cmd.Enabled = False
                numLevel.Enabled = False
            Case 1 ' ExecuteX10 Command
                txtDeviceCode.Enabled = True
                txtScript.Enabled = False
                cbX10Cmd.Enabled = True
            Case 2 ' Set Device Status

            Case 3 ' Set Device Value

        End Select
    End Sub

    Private Sub cbX10Cmd_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbX10Cmd.SelectedIndexChanged
        If cbX10Cmd.Text = "Dim" Then
            numLevel.Enabled = True
        Else
            numLevel.Enabled = False
        End If
    End Sub

    Private Sub cbDefinedActions_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbDefinedActions.SelectedIndexChanged
        Try
            Dim ActionID As Integer = cbDefinedActions.SelectedValue
            EditAction = cbDefinedActions.SelectedIndex
            cbActionType.SelectedIndex = a3.Rows(EditAction).Item("ActionType")
            If cbActionType.SelectedIndex = 0 Then
                txtScript.Text = a3.Rows(EditAction).Item("ActionCmd")
            Else
                txtScript.Text = ""
            End If
            If cbActionType.SelectedIndex = 1 Then
                cbX10Cmd.SelectedText = a3.Rows(EditAction).Item("ActionCmd")
            End If
            txtDeviceCode.Text = a3.Rows(EditAction).Item("DeviceCode")
            numLevel = a3.Rows(EditAction).Item("Level")
            EditAction = ActionID
        Catch ex As Exception
        End Try
    End Sub

    Private Sub btnNewAction_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNewAction.Click
        Dim Hi As Integer = a3.Rows(a3.Rows.Count - 1).Item("ActionID")
        Dim r As DataRow
        r = Actions.Tables(0).NewRow
        Hi += 1
        EditAction = a3.Rows.Count
        r.Item("ActionID") = Hi
        Actions.Tables(0).Rows.Add(r)
        txtNewActionDescription.Enabled = True
        txtNewActionDescription.Text = ""
        cbDefinedActions.Enabled = False
        cbActionType.Text = "Select Action Type"
        txtScript.Enabled = False
        txtScript.Text = ""
        cbX10Cmd.Enabled = False
        numLevel.Enabled = False
        numLevel.Value = 100
        txtDeviceCode.Enabled = False
        txtDeviceCode.Text = ""
    End Sub

    Private Sub btnCopyAction_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCopyAction.Click
        txtNewActionDescription.Enabled = True
        txtNewActionDescription.Text = ""
        cbDefinedActions.Enabled = True
        '        cbActionType.Text = "Select Action Type"
        txtScript.Enabled = False
        '       txtScript.Text = ""
        cbX10Cmd.Enabled = False
        numLevel.Enabled = False
        '      numLevel.Value = 100
        txtDeviceCode.Enabled = False
    End Sub

    Private Sub ActionConfig_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Me.Hide()
        e.Cancel = True
    End Sub

    Private Sub btnSaveAction_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveAction.Click
        With Actions.Tables(0).Rows(EditAction)
            .Item("Description") = txtNewActionDescription.Text
            .Item("DeviceCode") = txtDeviceCode.Text
            .Item("ActionType") = cbActionType.Text
            If cbActionType.SelectedIndex = 0 Then
                .Item("ActionCmd") = txtScript.Text
            Else
                .Item("ActionCmd") = cbX10Cmd.Text
            End If
            .Item("Level") = numLevel.Value
        End With
        WriteActions()
    End Sub

End Class